#!/usr/bin/python
#coding=utf-8
import urllib ,urllib2 ,socket ,os ,re ,net_traffic,work_port,sys
from file import montior_file
from process_total  import process_list
from CPU_LOAD import cpu_load, cpu_loadcore
from mem    import  meminfo 
import time,get_city_name
from disk import disk_usage ,disk_io
from squid.SquidCache import srv_squid

#提交函数
def post(url, data):
    data = urllib.urlencode(data)
    req = urllib2.Request(url, data)
    response = urllib2.urlopen(req, timeout=5)
    return response.read()

#入口函数
def main(server=None):
    data = {}

    posturl = "http://127.0.0.1:8102/recv/server"
    #posturl = "http://10.1.8.208:8102/recv/server"

    #获取地方市的名字
    #data['city'] = get_city_name.city()
    #data['city'] = '北京'

    #学校名称
    #data['school'] = '和盛大厦'

    #主机名
    if server:
        data['hostname'] = server.replace('_', '-')
    else:
        data['hostname'] = socket.gethostname().replace('_','-')

    #内存信息
    data['memload'] = meminfo.virtual_memory()
    #获取CPU进程数

    data['procnr'] = process_list()

    #文件监控
    data=dict(data, **montior_file._dict)

    #获取CPU_load值的百分比
    data['cpuload'] = cpu_loadcore.cpu_post()

    #获取网卡的流量,单位是KB
    data=dict(data, **net_traffic.interface_dict) 

    #业务端口监控
    data=dict(data, **work_port.port_zu) 

    #硬盘信息
    data=dict(data, **disk_usage._dict)

    #获取磁盘的IO
    data=dict(data, **disk_io._disk_io_dict)

    #获取Squid 缓存状态
    try:
        sys.argv[1]
    except IndexError:
        pass
    else:
        data['srv_squid'] = srv_squid()

    ret = post(posturl, data)

    #print ret
    return ret


if __name__ == '__main__':
    main()
